package com.stc.activity;


import java.util.ArrayList;
import java.util.HashMap;

import com.stc.pojo.Tactics;
import com.stc.utilities.DatabaseAdapter;

import android.app.ListActivity;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class ChooseTacticsActivity extends ListActivity {
		
	Cursor tacticsCursor;
	DatabaseAdapter myDBAdapter;
	ListView tacticsListView;
	static final ArrayList<HashMap<String,String>> tactics = new ArrayList<HashMap<String,String>>();
	TextView tacticsTitle;
	
	int raceId;
	String race = ""; 
		
	    @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.tacticslist);
	        
	        tacticsTitle = (TextView)findViewById(R.id.titleListTactics);
	        Bundle extras = getIntent().getExtras();
	        
	        if (extras != null) {
	        	race = extras.getString("race");
	        }

	        tacticsTitle.setText("Choose "+race+" tactics"); 
	        Log.d("tacticsTitle.getText(): ",String.valueOf(tacticsTitle.getText()));
	        
	        tacticsListView = (ListView)findViewById(android.R.id.list);
	        
	        //pobranie polaczenia do bazy
	        myDBAdapter = DatabaseAdapter.getHelper(getApplicationContext());
	        
	        //uzupelnienie listy
	        fillTacticsList();
	        fillListView();
	        
	    }
	    
	    private void fillTacticsList() {
	    	
	    	Log.d("race",race);
	    	
	    	if( race!=null && !"".equals(race)){
	    		if("Protos".equals(race))
	    			raceId = 1;
	    		if("Terran".equals(race))
	    			raceId = 2;
	    		if("Zerg".equals(race))
	    			raceId = 3;
	    	}
	    	
	    	Log.d("raceId",String.valueOf(raceId));
	    	
	    	tacticsCursor = myDBAdapter.getAllEntriesByRaceId(raceId);
	        startManagingCursor(tacticsCursor);
	        updateTacticsList();
	    }
	    
	    private void updateTacticsList() {
	    	tacticsCursor.requery();
	         
	    	tactics.clear();
	         
	        if(tacticsCursor.moveToFirst()) {
	            do {
	            	int rId = Integer.valueOf(tacticsCursor.getString(tacticsCursor.getColumnIndex(DatabaseAdapter.KEY_RACE_ID)));
	                String tactic = 
	                		tacticsCursor.getString(tacticsCursor.getColumnIndex(DatabaseAdapter.KEY_TACTIC));
	                String shortcut = 
	                		tacticsCursor.getString(tacticsCursor.getColumnIndex(DatabaseAdapter.KEY_SHORT_DESC));	
	                String idTactics = 
	                		tacticsCursor.getString(tacticsCursor.getColumnIndex(DatabaseAdapter.KEY_ID_T));	
	                //Tactics newTactic = new Tactics(tactic, shortcut, rId);
	                HashMap<String,String> tacticsTemp = new HashMap<String,String>();
	                tacticsTemp.put("tacticTitle",tactic);
	                tacticsTemp.put("tacticDesc",shortcut);
	                tacticsTemp.put("idTactics", idTactics);
	                tactics.add(tacticsTemp);
	            } while (tacticsCursor.moveToNext());
	        }
	    }
	    
	    private void fillListView() {
	   	    Log.d("tacticsListView", String.valueOf(tacticsListView));
	        
	    	SimpleAdapter simpleAdapter = new SimpleAdapter(
	        		this,
	        		tactics,
	        		R.layout.custom_row_tactic,
	        		new String[] {"tacticTitle","tacticDesc"},
	        		new int[] {R.id.tacticTitle,R.id.tacticDesc}
	        		); 
	        setListAdapter(simpleAdapter);
	    }
	    
	    @Override
    	protected void onListItemClick(ListView l, View v, int position, long id) {
    		super.onListItemClick(l, v, position, id);
    		// Get the item that was clicked
    		Object o = this.getListAdapter().getItem(position);
    		HashMap keyword = (HashMap)o;
    		//Toast.makeText(this, "You selected: " + keyword.get("tacticTitle"), Toast.LENGTH_LONG).show();
    		Intent intent = new Intent(this, ShowAndReadActivity.class);
    		intent.putExtra("race", race);
    		intent.putExtra("strategyName", ""+keyword.get("tacticTitle"));
    		intent.putExtra("strategyId", ""+keyword.get("idTactics"));
		     startActivity(intent);
    	}
	    
	    protected void onDestroy() {
	        myDBAdapter.close();
	        super.onDestroy();
	    }
}